package Aleehot100;

import java.util.Arrays;

/**
 * ClassName: c016
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/28 17:03
 * @Version 1.0
 * @Since 1.0
 */
public class c016 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4};
        int[] arr2 = {-1,1,0,-3,3};
        System.out.println(Arrays.toString(productExceptSelf(arr2)));
    }

    public static int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        if(n == 1) {
            return new int[]{0};
        }
        int[] ans = new int[n];
        ans[0] = nums[0];
        // 默认为左边的数据相乘的结果存入ans
        for (int i = 1; i < n; i++) {
            ans[i] = nums[i] * ans[i - 1];
        }

        // 统计结果
        int temp = 1;
        for (int i = n - 1; i > 0; i--) {
            ans[i] = ans[i - 1] * temp;
            temp *= nums[i];
        }
        ans[0] = temp;
        return ans;
    }

}
